🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / common / src / commonMain / kotlin / org / meshtastic / core / common / util / SequentialJob.kt
Displaying Raw • Download
core/common/src/commonMain/kotlin/org/meshtastic/core/common/util/SequentialJob.kt 2d20cd8a4708e2ef66e98d5259f3a97ec93f240a (2d20cd8a) Text, 2.57 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.common.util
Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.atomicfu.atomic
Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787kotlinx.coroutines.Job
Tff7b72import T7ee787kotlinx.coroutines.TimeoutCancellationException
Tff7b72import T7ee787kotlinx.coroutines.withTimeout
Tff7b72import T7ee787org.koin.core.annotation.Factory
T8b949e/**
* A helper class that manages a single [Job]. When a new job is launched, any previous job is cancelled. This is useful
* for ensuring that only the latest operation of a certain type is running at a time (e.g. for search or settings
* updates).
*/
Tf0883e@Factory
Tff7b72class T56d364SequentialJob Tb4b4b4{
Tff7b72private Tff7b72val Te6edf3job Tff7b72= Te6edf3atomicTff7b72<Te6edf3Job?Tff7b72>Tb4b4b4(Tff7b72nullTb4b4b4)
T8b949e/**
* Cancels the previous job (if any) and launches a new one in the given [scope]. The new job uses [handledLaunch]
* to ensure exceptions are reported.
*
* @param timeoutMs Optional timeout in milliseconds. If > 0, the [block] is wrapped in [withTimeout] so that
* indefinitely-suspended coroutines (e.g. blocked DataStore reads) throw [TimeoutCancellationException] instead
* of hanging silently.
*/
Tff7b72fun Td2a8fflaunchTb4b4b4(Te6edf3scopeTb4b4b4: Te6edf3CoroutineScopeTb4b4b4, Te6edf3timeoutMsTb4b4b4: Tffa657Long Tff7b72= T79c0ff0Tb4b4b4, Te6edf3blockTb4b4b4: Te6edf3suspend Te6edf3CoroutineScopeTb4b4b4.Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4) Tb4b4b4{
Te6edf3cancelTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3newJob Tff7b72=
Te6edf3scopeTb4b4b4.Te6edf3handledLaunch Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3timeoutMs Tff7b72> T79c0ff0Tb4b4b4) Tb4b4b4{
Tff7b72try Tb4b4b4{
Te6edf3withTimeoutTb4b4b4(Te6edf3timeoutMsTb4b4b4, Te6edf3blockTb4b4b4)
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3eTb4b4b4: Te6edf3TimeoutCancellationExceptionTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Ta5d6ffSequentialJob timed out after Tffd700${Te6edf3timeoutMsTffd700}Ta5d6ffmsTa5d6ff" Tb4b4b4}
Tff7b72throw Te6edf3e
Tb4b4b4}
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3blockTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Te6edf3jobTb4b4b4.Te6edf3value Tff7b72= Te6edf3newJob
Te6edf3newJobTb4b4b4.Te6edf3invokeOnCompletion Tb4b4b4{ Te6edf3jobTb4b4b4.Te6edf3compareAndSetTb4b4b4(Te6edf3newJobTb4b4b4, Tff7b72nullTb4b4b4) Tb4b4b4}
Tb4b4b4}
T8b949e/** Cancels the current job. */
Tff7b72fun Td2a8ffcancelTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3jobTb4b4b4.Te6edf3getAndSetTb4b4b4(Tff7b72nullTb4b4b4)Tff7b72?.Te6edf3cancelTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.04s